/*----------- start Block -----------*/
var BLOCK_NORMAL = 0;
var BLOCK_DAMAGED_1 = 1;
var BLOCK_DAMAGED_2 = 2;
var BLOCK_DAMAGED_3 = 3;

var BLOCK_TYPE_1_2 = [[1, 2], [4, 12], [5, 12], [6, 12]];
var BLOCK_TYPE_2_2 = [[2, 2], [7, 12], [9, 12], [11, 12]];
var BLOCK_TYPE_3_2 = [[3, 2], [8, 12], [10, 12], [12, 12]];
var BLOCK_TYPE_4_2 = [[4, 2], [4, 8], [5, 8], [6, 8]];
var BLOCK_TYPE_5_2 = [[5, 2], [1, 8], [2, 8], [3, 8]];
var BLOCK_TYPE_6_2 = [[6, 2], [7, 8], [8, 8], [9, 8]];
var BLOCK_TYPE_7_2 = [[7, 2], [1, 12], [2, 12], [3, 12]];
var BLOCK_TYPE_8_2 = [[8, 2], [13, 1], [13, 2], [13, 3]];
var BLOCK_TYPE_9_2 = [[9, 2], [14, 1], [14, 2], [14, 3]];
var BLOCK_TYPE_10_2 = [[10, 2], [1, 11], [2, 11], [3, 11]];
var BLOCK_TYPE_11_2 = [[11, 2], [10, 8], [11, 8], [12, 8]];
var BLOCK_TYPE_12_2 = [[12, 2], [1, 9], [2, 9], [3, 9]];

var BLOCK_TYPE_1_3 = [[1, 3], [1, 3], [1, 3], [1, 3]];
var BLOCK_TYPE_2_3 = [[2, 3], [2, 3], [2, 3], [2, 3]];
var BLOCK_TYPE_3_3 = [[3, 3], [7, 9], [8, 9], [9, 9]];
var BLOCK_TYPE_4_3 = [[4, 3], [7, 10], [8, 10], [9, 10]];
var BLOCK_TYPE_5_3 = [[5, 3], [4, 9], [5, 9], [6, 9]];
var BLOCK_TYPE_6_3 = [[6, 3], [13, 4], [13, 5], [13, 6]];
var BLOCK_TYPE_7_3 = [[7, 3], [14, 4], [14, 5], [14, 6]];
var BLOCK_TYPE_8_3 = [[8, 3], [10, 10], [11, 10], [12, 10]];
var BLOCK_TYPE_9_3 = [[9, 3], [4, 11], [5, 11], [6, 11]];
var BLOCK_TYPE_10_3 = [[10, 3], [7, 11], [8, 11], [9, 11]];
var BLOCK_TYPE_11_3 = [[11, 3], [10, 11], [11, 11], [12, 11]];
var BLOCK_TYPE_12_3 = [[12, 3], [13, 10], [14, 10], [15, 10]];

var BLOCK_TYPE_1_4 = [[1, 4], [13, 7], [13, 8], [13, 9]];
var BLOCK_TYPE_2_4 = [[2, 4], [14, 7], [14, 8], [14, 9]];
var BLOCK_TYPE_3_4 = [[3, 4], [10, 9], [11, 9], [12, 9]];
var BLOCK_TYPE_12_4 = [[12, 4], [13, 11], [14, 11], [15, 11]];

var BLOCK_TYPE_1_5 = [[1, 5], [1, 10], [2, 10], [3, 10]];
var BLOCK_TYPE_2_5 = [[2, 5], [4, 10], [5, 10], [6, 10]];

var blocksInit = function(){
    var index = 0;
    for (var i = 1; i < 13; i++) {
        for (var j = 1; j < 13; j++) {
            if (window['BLOCK_TYPE_' + j + '_' + i]) {
                window['BLOCK_TYPE_' + index++] = window['BLOCK_TYPE_' + j + '_' + i];
            }
        }
    } 
}();


function Block(blockType, x, y, collisionManager) {
    this.collisionManager = collisionManager;
	this.blockType = blockType;
    this.x = x;
    this.y = y;
    this.halfWidth = 15;
    this.halfHeight = 15;
    this.x1 = this.x - this.halfWidth;
    this.x2 = this.x + this.halfWidth;
    this.y1 = this.y - this.halfHeight
    this.y2 = this.y + this.halfHeight
    this.dx = 0;
    this.dy = 0;
    this.remove = false;
    this.visible = true;
    this.life = 4;

    var bnormal = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: blockType[0][0],
        y: blockType[0][1],
        offsetX: 16,
        offsetY: 16
    });

    var bdamaged1 = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: blockType[1][0],
        y: blockType[1][1],
        offsetX: 16,
        offsetY: 16
    });

    var bdamaged2 = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: blockType[2][0],
        y: blockType[2][1],
        offsetX: 16,
        offsetY: 16
    });

    var bdamaged3 = new ImageSprite(IMAGE, {
        cellWidth: 32,
        cellHeight: 32,
        gridSpace: 1,
        x: blockType[3][0],
        y: blockType[3][1],
        offsetX: 16,
        offsetY: 16
    });

    this.sprites = [bdamaged3, bdamaged2, bdamaged1, bnormal];

    this.spriteIndex = BLOCK_NORMAL;
    this.collisionManager.objects.push(this);
}

Block.prototype.update = function() {
};

Block.prototype.render = function(context) {
    if (this.visible) {
        this.spriteIndex = this.life - 1;
        this.sprites[this.spriteIndex].x = this.x;
        this.sprites[this.spriteIndex].y = this.y;
        this.sprites[this.spriteIndex].render(context);
    }
};

Block.prototype.onCollide = function(obj) {
    if (obj instanceof Projectile) {
        this.life--;
    }
    if (this.life <= 0) {
        this.remove = true;
        this.visible = false;
    }
};
/*----------- end Block -----------*/